package com.bootdo.staff.service.impl;

import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.BuildTree;
import com.bootdo.system.domain.MenuDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.staff.dao.StaffMenuDao;
import com.bootdo.staff.dao.StaffMenuRoleDao;
import com.bootdo.staff.domain.StaffMenuDO;
import com.bootdo.staff.service.StaffMenuService;
import org.springframework.transaction.annotation.Transactional;

@SuppressWarnings("AlibabaRemoveCommentedCode")
@Service
@Transactional(readOnly = false,rollbackFor = Exception.class)
public class StaffMenuServiceImpl implements StaffMenuService {
	@Autowired
	private StaffMenuDao staffMenuDao;
	@Autowired
	private StaffMenuRoleDao staffMenuRoleDao;
	
	@Override
	public StaffMenuDO get(Long smenuId){
		return staffMenuDao.get(smenuId);
	}
	
	@Override
	public List<StaffMenuDO> list(Map<String, Object> map){
		return staffMenuDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return staffMenuDao.count(map);
	}
	
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	@Override
	public int save(StaffMenuDO staffMenu){
		return staffMenuDao.save(staffMenu);
	}
	
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	@Override
	public int update(StaffMenuDO staffMenu){
		return staffMenuDao.update(staffMenu);
	}
	
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	@Override
	public int remove(Long smenuId){
		return staffMenuDao.remove(smenuId);
	}
	
	@Override
	public int batchRemove(Long[] smenuIds){
		return staffMenuDao.batchRemove(smenuIds);
	}



	@Override
	public List<StaffMenuDO> getMenuByStaffId(Long staffId){
		return staffMenuDao.getMenuByStaffId(staffId);
	}
	

	@Override
	public Tree<StaffMenuDO> getTree() {
		List<Tree<StaffMenuDO>> trees = new ArrayList<Tree<StaffMenuDO>>();
		List<StaffMenuDO> menuDOs = staffMenuDao.list(new HashMap<>(16));
		for (StaffMenuDO sMenuDO : menuDOs) {
			Tree<StaffMenuDO> tree = new Tree<StaffMenuDO>();
			tree.setId(sMenuDO.getSmenuId().toString());
			tree.setParentId(sMenuDO.getParentId().toString());
			tree.setText(sMenuDO.getMenuName());
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<StaffMenuDO> t = BuildTree.build(trees);
		return t;
	}

	@Override
	public Tree<StaffMenuDO> getTree(Long id) {
				// 根据staffId查询权限
				List<StaffMenuDO> smenus = staffMenuDao.list(new HashMap<String, Object>(16));
				List<Long> smenuIds = staffMenuRoleDao.listMenuIdByUserId(id);
				List<Long> temp = smenuIds;
				for (StaffMenuDO menu : smenus) {
					if (temp.contains(menu.getParentId())) {
						smenuIds.remove(menu.getParentId());
					}
				}
				List<Tree<StaffMenuDO>> trees = new ArrayList<Tree<StaffMenuDO>>();

				List<StaffMenuDO> smenuDOs = staffMenuDao.list(new HashMap<String, Object>(16));
				for (StaffMenuDO sMenuDO : smenuDOs) {
					Tree<StaffMenuDO> tree = new Tree<StaffMenuDO>();
					tree.setId(sMenuDO.getSmenuId().toString());
					tree.setParentId(sMenuDO.getParentId().toString());
					tree.setText(sMenuDO.getMenuName());
					Map<String, Object> state = new HashMap<>(16);
					Long menuId = sMenuDO.getSmenuId();
					if (smenuIds.contains(menuId)) {
						state.put("selected", true);
					} else {
						state.put("selected", false);
					}
					tree.setState(state);
					trees.add(tree);
				}
				// 默认顶级菜单为０，根据数据库实际情况调整
				Tree<StaffMenuDO> t = BuildTree.build(trees);
				return t;
	}
}
